<template>
  <div class="information">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>药材管理</el-breadcrumb-item>
      <el-breadcrumb-item>药材信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索和添加 -->
      <el-row type="flex" class="row-bg" justify="space-around">
        <el-col :span="16">
          <el-button icon="el-icon-circle-plus-outline" @click="addMedicineDialogVisible = true">新增药材</el-button>
        </el-col>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="info.query" clearable @clear="getMedicineList">
            <el-button slot="append" icon="el-icon-search" @click="getMedicineList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 药材列表 -->
      <el-table :data="medicineList" style="width: 100%" border>
        <el-table-column prop="id" label="编号" width="120">
        </el-table-column>
        <el-table-column prop="ycName" label="药材名称">
        </el-table-column>
        <el-table-column prop="ycAmount" label="药材数量(克)">
        </el-table-column>
        <el-table-column prop="ycPrice" label="药材单价(元/克)">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="info" icon="el-icon-edit" @click="showEditMedicineDialog(scope.row.id)">修改信息</el-button>
            <el-button type="warning" icon="el-icon-delete" @click="deleteMedicine(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="info.pagenum"
        :page-sizes="[4, 6, 8, 9]" :page-size="info.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 新增药材的对话框 -->
    <el-dialog title="新增药材" :visible.sync="addMedicineDialogVisible" width="30%" @close="addMedicineDialogClosed">
      <el-form :model="addMedicineForm" :rules="addMedicineFormRules" ref="addMedicineFormRef" label-width="140px">
        <el-form-item label="请输入药材名称" prop="ycName">
          <el-input v-model="addMedicineForm.ycName"></el-input>
        </el-form-item>
        <el-form-item label="请输入药材数量" prop="ycAmount">
          <el-input v-model="addMedicineForm.ycAmount" type="number"></el-input>
        </el-form-item>
        <el-form-item label="请输入药材单价" prop="ycPrice">
          <el-input v-model="addMedicineForm.ycPrice" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMedicineDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMedicine">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改药材的对话框 -->
    <el-dialog title="修改药材" :visible.sync="editMedicineDialogVisible" width="30%" @close="editMedicineDialogClosed">
      <el-form :model="editMedicineForm" :rules="editMedicineFormRules" ref="editMedicineFormRef" label-width="140px">
        <el-form-item label="请输入药材名称" prop="ycName">
          <el-input v-model="editMedicineForm.ycName"></el-input>
        </el-form-item>
        <el-form-item label="请输入药材数量" prop="ycAmount">
          <el-input v-model="editMedicineForm.ycAmount" type="number"></el-input>
        </el-form-item>
        <el-form-item label="请输入药材单价" prop="ycPrice">
          <el-input v-model="editMedicineForm.ycPrice" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editMedicineDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editMedicine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        //药材信息列表
        info: {
          query: '',
          pageNum: 1,
          pageSize: 4
        },
        medicineList: [],
        //总条数
        total: 0,
        //控制新增药材对话框显示
        addMedicineDialogVisible: false,
        //新增药材表单
        addMedicineForm: {
          ycName: '',
          ycAmount: 0,
          ycPrice: 0
        },
        addMedicineFormRules: {
          ycName: [{
            required: true,
            message: '请输入药材名称',
            trigger: 'blur'
          }],
          ycAmount: [{
            required: true,
            message: '请输入药材数量',
            trigger: 'blur'
          }],
          ycPrice: [{
            required: true,
            message: '请输入药材单价',
            trigger: 'blur'
          }]
        },
        //控制编辑药材的对话框显示
        editMedicineDialogVisible: false,
        editMedicineForm: {},
        editMedicineFormRules: {
          ycName: [{
            required: true,
            message: '请输入药材名称',
            trigger: 'blur'
          }],
          ycAmount: [{
            required: true,
            message: '请输入药材数量',
            trigger: 'blur'
          }],
          ycPrice: [{
            required: true,
            message: '请输入药材单价',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      this.getMedicineList()
    },
    methods: {
      async getMedicineList() {
        const {
          data: res
        } = await this.$http.post('/information', this.info)
        this.medicineList = res.data.list
        this.total = res.data.total
      },
      //每页显示条数变化的事件
      handleSizeChange(size) {
        this.info.pageSize = size
        this.getMedicineList()
      },
      //监听页码改变
      handleCurrentChange(page) {
        this.info.pageNum = page
        this.getMedicineList()
      },
      //监听新增药材对话框关闭
      addMedicineDialogClosed() {
        this.$refs.addMedicineFormRef.resetFields()
      },
      //提交新增药材表单
      addMedicine() {
        this.$refs.addMedicineFormRef.validate(async valid => {
          if (!valid) return
          const res = await this.$http.post('/information/add', this.addMedicineForm)
          if(res.data.status === false) {
            this.addMedicineDialogVisible = false
            this.$message.error('药材已经存在！')
            return
          }
          console.log(res)
          this.addMedicineDialogVisible = false
          this.$message.success('新增药材成功！')
          this.getMedicineList()
        })
      },
      //修改药材信息的对话框
      async showEditMedicineDialog(id) {
        const {
          data: res
        } = await this.$http.get('/information/' + id)
        this.editMedicineForm = res.data
        this.editMedicineDialogVisible = true
      },
      editMedicineDialogClosed() {
        this.$refs.editMedicineFormRef.resetFields()
      },
      //提交修改药材表单
      async editMedicine() {
        this.$refs.editMedicineFormRef.validate(async valid => {
          if (!valid) return
          const {
            data: res
          } = await this.$http.post('/information/modify', this.editMedicineForm)
          if (res.status === false) {
            this.$message.error('修改药材失败！')
          }
          this.editMedicineDialogVisible = false
          this.$message.success('修改药材成功！')
          this.getMedicineList()
        })
      },
      //删除药材
      async deleteMedicine(id) {
         const opRes = await this.$confirm('此操作将永久删除该药材信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(opRes === 'confirm') {
          const { data: res } = await this.$http.get('/del/' + id)
          this.$message.success('删除药材成功！')
          this.getMedicineList()
        }
      }
    }
  }
</script>

<style>

</style>
